<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/8
 * Time: 15:32
 */



$oldPwd = $_POST['oldPwd'];
$newPwd = $_POST['newPwd'];

$key = $_COOKIE["userID"];

echo $username;

session_start();

$username = $_SESSION[$key]['username'];
if(!isset($_SESSION[$key])) {
    echo "你尚未登入，<a href='../login.php'>请登入</a>";
    die;
}else{
    if ($key==md5($username.$oldPwd)){
        if ($newPwd==$oldPwd){
            exit("旧密码与新密码不可相同！");
        }
        if (empty($newPwd)) {
            echo '新密码不能为空！';
            die;
        }
        $key = md5($username.$newPwd);
        setcookie("userID",$key,time()+24*3600,'/');
        $_SESSION[$key]=[
            'username'=>$username,
            'psd'=>$newPwd
        ];
        echo "<h1>修改成功</h1>";
        echo '<br>';
        echo '<a href="userInfo.php">用户中心</a>';
    }else{
        if (empty($oldPwd)){
            echo '旧密码不能为空！';
            die;
        }
        echo "旧密码输入错误！";
    }
}


